package com.xnfh.common.manager

import android.util.Log
import com.xnfh.common.constant.KvKey
import com.xnfh.framework.Constants
import com.xnfh.framework.bean.UserInforBean
import com.xnfh.framework.utils.KVUtil

/**
 * @Description:
 * @Author: ziheng
 * @CreateDate: 2023/11/10 11:35
 * @UpdateUser: ziheng
 * @UpdateDate: 2023/11/10 11:35
 * @UpdateRemark: 更新说明
 */
class UserInfoManager private constructor() {

    private val TAG = "UserInfoManager"

    companion object {
        //双重锁校验模式
        val instance: UserInfoManager by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            UserInfoManager()
        }
    }

    /**
     * 更新用户信息
     */
    fun updateUserInfor(userInfo: UserInforBean?) {
        userInfo?.let {
            setUserId(userInfo.udu_id)
            KVUtil.putParcelable(KvKey.USER_INFO, it)
        }
    }

    /**
     * 获取用户信息
     */
    fun getUserInfo(): UserInforBean? {
        return KVUtil.getParcelable(KvKey.USER_INFO)
    }

    /**
     * 获取用户是否登录
     */
    fun isLogin(): Boolean {
        Log.e(TAG, "isLogin: ${getUserInfo().toString()}")
        return getUserInfo() != null
    }

    /**
     * 设置用户id
     */
    private fun setUserId(userId: String?) {
        KVUtil.put(KvKey.USER_ID, userId)
    }
    /**
     * 获取用户id
     */
    fun getUserId(): String?{
        return KVUtil.getString(KvKey.USER_ID)
    }


    /**
     * 设置用户手机号
     */
    fun setUserPhone(phone: String){
        KVUtil.put(KvKey.USER_PHONE,phone)
    }

    /**
     * 获取用户手机号
     */
    fun getUserPhone(): String?{
        return KVUtil.getString(KvKey.USER_PHONE)
    }

    /**
     * 设置用户头像
     */
    fun setUserHeader(url: String){
        KVUtil.put(KvKey.USER_HEADER,url)
    }

    /**
     * 获取用户头像
     */
    fun getUserHeader(): String?{
        return KVUtil.getString(KvKey.USER_HEADER)
    }


    fun loginOut(){
        KVUtil.remove(KvKey.USER_ID)
        KVUtil.remove(KvKey.USER_INFO)
    }
}